package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.product.pojo.param.AttributeTemplateInsertParam;
import cn.tedu.csmall.product.pojo.param.AttributeTemplateUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateListVO;
import cn.tedu.csmall.product.service.AttributeTemplateService;
import cn.tedu.csmall.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/attributeTemplate")
@Api(tags = "06.属性模版管理模块")
@Validated
@Slf4j
public class AttributeTemplateController {
    @Autowired
    AttributeTemplateService service;
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    @ApiOperation("添加属性模版")
    @ApiOperationSupport(order = 100)
    public JsonResult insert(@Valid AttributeTemplateInsertParam AttributeTemplateInsertParam){
            service.insert(AttributeTemplateInsertParam);
            return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("根据ID删除属性模版")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "attributeTemplateId", value = "属性模版ID", required = true, dataType = "long"),

    })
    public JsonResult delete(@Range(min = 1,message = "根据ID删除属性模版失败，请提交合法的ID值")
            @RequestParam Long attributeTemplateId) {
       service.deleteById(attributeTemplateId);
       return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改属性模版")
    @ApiOperationSupport(order = 300)
    public JsonResult update(@Valid AttributeTemplateUpdateInfoParam attributeTemplateUpdateInfoParam) {
        service.update(attributeTemplateUpdateInfoParam);
        return JsonResult.ok();
    }


    @GetMapping("/list")
    @ApiOperation("查看属性模版")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = false,paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult list(@Range(min=1,message = "请输入合法的页码") Integer pageNum,
                           String queryType){
        if(pageNum==null){
            pageNum=1;
        }

        PageData<AttributeTemplateListVO> pageData;
        if ("all".equals(queryType)) {
            pageData = service.list(pageNum, Integer.MAX_VALUE);
        } else {
            pageData = service.list(pageNum);
        }

        return JsonResult.ok(pageData);
    }

    @GetMapping("/getStandard")
    @ApiOperation("根据ID查询属性模版详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "属性模版ID", required = true, dataType = "long")
    })
    public JsonResult getStandard(@Range(min = 1,message = "根据ID查看属性模版详情失败，请提交合法的ID值")
                                  @RequestParam Long id) throws Exception {
        return JsonResult.ok(service.getStandardById(id));
    }
}
